<template>
  <div>
    <!-- 搜索页面头部 -->
    <div class="search-header">
      <!-- 后退按钮 -->
      <van-icon
        name="arrow-left"
        color="white"
        size="0.48rem"
        class="goback"
        @click="$router.back()"
      />
      <!-- 搜索组件 -->
      <van-search
        v-model="kw"
        v-focus
        placeholder="请输入搜索关键词"
        background="#007BFF"
        shape="round"
        @input="inputFn"
        @search="seleFn(kw)"
      />
    </div>
    <!-- 搜索建议列表 -->
    <div class="sugg-list" v-if="kw.length !== 0">
      <div
        class="sugg-item"
        v-for="(itm, i) in List"
        :key="i"
        v-html="Reg(itm)"
        @click="seleFn(itm)"
      ></div>
    </div>
    <!-- 搜索历史 -->
    <div class="search-history" v-else>
      <!-- 标题 -->
      <van-cell title="搜索历史">
        <!-- 使用 right-icon 插槽来自定义右侧图标 -->
        <template #right-icon>
          <van-icon name="delete" class="search-icon" @click="selectFn" />
        </template>
      </van-cell>

      <!-- 历史列表 -->
      <div class="history-list">
        <span
          class="history-item"
          v-for="(itm, i) in history"
          :key="i"
          @touchstart="touchstartFn(i)"
          @touchmove="touchmoveFn"
          @touchend="touchendFn"
          @click="seleFn(itm)"
          >{{ itm }}</span
        >
      </div>
    </div>
  </div>
</template>

<script>
import { GetSuggestion } from '@/api/home'
import { Dialog } from 'vant'
export default {
  data () {
    return {
      kw: '', // 搜索关键字
      List: [],
      history: JSON.parse(localStorage.getItem('history')) || [] // 搜索历史
    }
  },
  methods: {
    touchstartFn (i) {
      this.Tiem = setTimeout(async () => {
        try {
          await Dialog.confirm({
            message: '确定删除?'
          })
          this.history.splice(i, 1)
        } catch (error) {
          console.log('为甚?')
        }
      }, 800)
    },
    touchmoveFn () {
      clearTimeout(this.Tiem)
    },
    touchendFn () {
      clearTimeout(this.Tiem)
    },
    inputFn () {
      clearTimeout(this.time)
      this.time = setTimeout(async () => {
        if (this.kw.length === 0) return
        const res = await GetSuggestion(this.kw)
        const options = res.data.options
        this.List = options.map((itm) => itm.replaceAll(' ', ''))
      }, 500)
    },
    Reg (itm) {
      const reg = new RegExp(this.kw, 'ig')
      return itm.replace(reg, (mav) => {
        return `<span style="color:red;">${mav}</span>`
      })
    },
    async selectFn () {
      try {
        await Dialog.confirm({
          message: '确定删除麽?'
        })
        this.history = []
      } catch (error) {
        console.log('为什么不删除?')
      }
    },
    seleFn (ser) {
      if (!this.history.includes(ser)) {
        this.history.push(ser)
      }
      setTimeout(() => {
        this.$router.push({
          path: '/search/' + ser
        })
      })
      this.kw = ''
    }
  },
  watch: {
    history () {
      localStorage.setItem('history', JSON.stringify(this.history))
    }
  }
}
</script>

<style scoped lang="less">
.search-header {
  height: 46px;
  display: flex;
  align-items: center;
  background-color: #007bff;
  overflow: hidden;
  /*后退按钮*/
  .goback {
    padding-left: 14px;
  }
  /*搜索组件*/
  .van-search {
    flex: 1;
  }
}
/*搜索建议列表样式 */
.sugg-list {
  .sugg-item {
    padding: 0 15px;
    border-bottom: 1px solid #f8f8f8;
    font-size: 14px;
    line-height: 50px;
    // 实现省略号的三行代码
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
/**搜索历史 */
.search-icon {
  font-size: 16px;
  line-height: inherit;
}

.history-list {
  padding: 0 10px;
  .history-item {
    display: inline-block;
    font-size: 12px;
    padding: 8px 14px;
    background-color: #efefef;
    margin: 10px 8px 0px 8px;
    border-radius: 10px;
  }
}
</style>
